#!/bin/sh
rm -rf rpmtmp/
PKGNAMEPREFIX=openpanel
export PKGNAMEPREFIX
ARCH="$1"
SKIP="$2"
if [ -z "$ARCH" ]; then
  echo "Usage: $0 <arch>"
  exit 1
fi
if [ ! "$SKIP" = "skiptar" ]; then
echo -n "* Creating tarballs: "
bin/maketars tipmost > /var/log/openpanel-build.log 2>&1 || {
  echo "FAIL"
  exit 1
}
echo "OK"
fi
echo -n "* Building and installing openpanel-devel: "
graceid=`ls -1 tartmp/libgrace-*.tar.gz | grep "libgrace-[0-9]" | sed -e "s/^tartmp.//;s/\.tar\.gz$//"`
bin/mkrpm tartmp/openpanel-devel-*.tar.gz ${ARCH} >> /var/log/openpanel-build.log 2>&1 || {
  echo "FAIL"
  exit 1
}
bin/mkrpm tartmp/openpanel-mkmodulexml-*.tar.gz ${ARCH} >> /var/log/openpanel-build.log 2>&1 || {
  echo "FAIL"
  exit 1
}
bin/mkrpm tartmp/${graceid}.tar.gz >> /var/log/openpanel-build.log 2>&1 || {
  echo "FAIL"
  exit 1
}
rpm -Uvh rpmtmp/RPMS/i386/openpanel-devel-*.rpm rpmtmp/RPMS/i386/openpanel-mkmodulexml-*.rpm rpmtmp/RPMS/i386/${graceid}*.rpm >>/var/log/openpanel-build.log 2>&1
echo "OK"
for pkg in tartmp/*.tar.gz; do
  pkgname=`echo "$pkg" | sed -e "s/.tar.gz$//"`
  echo -n "* $pkgname: "
  bin/mkrpm $pkg ${ARCH} >> /var/log/openpanel-build.log 2>&1 || {
    echo "FAIL"
    exit 1
  }
  echo "OK"
done
